package edu.cs.usask.ca.silver.plugin.rcp.pages.preferences;

import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import edu.cs.usask.ca.silver.definitions.SystemProperties;
import edu.cs.usask.ca.silver.plugin.rcp.Activator;
import edu.cs.usask.ca.silver.plugin.rcp.components.SilverPreferenceEntries;

public class DatabasePreferencePage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	public DatabasePreferencePage() {
		super(GRID);
	}

	public void createFieldEditors() {
		addField(new RadioGroupFieldEditor("DBCHOICE",
				"Choose the type of database to use.", 1, new String[][] {
						{ "&Connect to a local database.", "local" },
						{ "&Connect to a remote database.", "remote" } },
				getFieldEditorParent()));
		addField(new StringFieldEditor(
				SilverPreferenceEntries.DATABASE_LOCATION.name(),
				SilverPreferenceEntries.DATABASE_LOCATION
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.DATABASE_LOCATION
						.getLabelWidthToBeArranged(), getFieldEditorParent()));
		addField(new StringFieldEditor(
				SilverPreferenceEntries.DATABASE_NAME.name(),
				SilverPreferenceEntries.DATABASE_NAME
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.DATABASE_NAME
						.getLabelWidthToBeArranged(), getFieldEditorParent()));
		addField(new StringFieldEditor(
				SilverPreferenceEntries.DATABASE_USER.name(),
				SilverPreferenceEntries.DATABASE_USER
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.DATABASE_USER
						.getLabelWidthToBeArranged(), getFieldEditorParent()));
		addField(new StringFieldEditor(
				SilverPreferenceEntries.DATABASE_PASSWORD.name(),
				SilverPreferenceEntries.DATABASE_PASSWORD
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.DATABASE_PASSWORD
						.getLabelWidthToBeArranged(), getFieldEditorParent()));
		addField(new StringFieldEditor(
				SilverPreferenceEntries.DATABASE_PORT.name(),
				SilverPreferenceEntries.DATABASE_PORT
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.DATABASE_PORT
						.getLabelWidthToBeArranged(), getFieldEditorParent()));

		// path for local database?
		
		// addField(new DirectoryFieldEditor("H2PATH",
		// "Path for local Database:",
		// getFieldEditorParent()));
	}

	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("Database Configuration for remote database "
				+ (SystemProperties.OS_NAME.startsWith("Windows") ? "'My Documents'"
						: "'~' folder."));
	}

	@Override
	public boolean performOk() {
		boolean result = super.performOk();
		// reset database connection?
		return result;
	}

}
